package org.theqcp.wow.utilities.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HTTPRead {

	public static String getHTTPFromURL(URL xmlURL) throws IOException {
		
		return HTTPRead.getHTTPFromURL(xmlURL, null);
		
	}
	
	public static String getHTTPFromURL(URL xmlURL, String userAgent) throws IOException {
		
		//init xml message
		String xml = "";
				
		//connection 
		HttpURLConnection connection = (HttpURLConnection)xmlURL.openConnection();
	
		//if a user agent is available, set it
		if(userAgent != null && !userAgent.isEmpty()) {
	        // Set User-Agent request header 
			connection.setRequestProperty("User-Agent", userAgent); 
		}
		
		BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

		String inputLine;
	
		while ((inputLine = in.readLine()) != null) {
			xml += inputLine;
		}
	
		in.close();
		
		//return grabbed message
		return xml;
	}
	
}
